import time,pymysql

from dama import Dama
from PIL import Image
# from data import db
from selenium import webdriver
from selenium.webdriver.common.by import By

# sql = db('root','yidong666','10.59.9.12','a3mall')

driver=webdriver.Edge()
driver.get('http://10.59.9.12:7001/')
driver.maximize_window()
time.sleep(3)

driver.find_element(By.XPATH,'/html/body/div[1]/div/div[2]/ul/li[1]/a').click()
time.sleep(3)
driver.find_element(By.NAME,'username').send_keys('13811033962')
time.sleep(3)
driver.find_element(By.NAME,'password').send_keys('123456')
time.sleep(10)
# ye = sql.select('select amount from mall_users where username = "13811033962"')
# print(ye[0][0])
driver.find_element(By.XPATH,'//*[@id="theForm"]/div[5]/a').click()
time.sleep(5)
driver.switch_to.window(driver.window_handles[-1])
time.sleep(2)

#截取大图
datu=driver.save_screenshot('datu.png')
#获取验证码图的位置
yzmtu=driver.find_element(By.XPATH,'//*[@id="theForm"]/div[3]/span[3]/img')

# 位置
location = yzmtu.location
print(location)
# 大小
size = yzmtu.size
print(size)

left=location['x']
top=location['y']
right=left+size['width']
bottom=top+size['height']
#先打开大图
image=Image.open('../datu.png')
#再根据分辨率获取小图
imagex=image.crop((left*1.5,top*1.5,right*1.5,bottom*1.5))
#返回验证码小图
imagex.save('imageyzm.png')

#初始化打码工具
da=Dama('grq13381144084','123456','972479')
#读取验证码截图r是读
yzmda=open('../imageyzm.png', 'rb').read()
#打码工具方法读取内容
jieguo=da.PostPic(yzmda,'1902')
#返回读取结果
print(jieguo)
#返回验证码
result=jieguo['pic_str']
time.sleep(3)
#输入验证码
driver.find_element(By.XPATH,'//*[@id="theForm"]/div[3]/span[2]/input').send_keys(result)
#点击请登录按钮
driver.find_element(By.CLASS_NAME,'finish-btn').click()
time.sleep(5)






# dy3=driver.find_element(By.XPATH,'/html/body/div[5]/div[2]/div[1]/div[2]/div[2]/ul/li[4]/span').text
# print(dy3.replace('¥ ',''))
#
# db().select('FLUSH TABLES')
#
#
# assert float(dy3.replace('¥ ',''))==float(ye[0][0])